package com.controller;

import com.alibaba.fastjson.JSON;
import com.bean.JwtOperation;
import com.constant.AppConstant;
import com.dto.JsonResult;
import com.dto.User;
import com.entity.Role;
import com.exception.DisabledLoginException;
import com.service.RoleService;
import com.vo.ConstantVo;
import com.vo.RoleVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author Yadasu
 * @date 2020/12/21 16:49
 */
@RestController
public class RoleController {


    @Autowired
    private RoleService roleService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 获取角色信息
     * @return
     */
    @GetMapping("/api/admin/role")
    public JsonResult listRoles() {
        return JsonResult.buildSuccess(200, roleService.findAllRoles());
    }

    /**
     *角色可用状态
     * @return
     */
    @PutMapping("/api/admin/role/status")
    public JsonResult updateRoleStatus(@RequestBody Role requestRole) {
        Role role = roleService.updateRoleStatus(requestRole);
        String message = "用户" + role.getNameZh() + "状态更新成功";
        return JsonResult.buildSuccess(200, message);
    }

    /**
     *角色详细信息？
     * @return
     */
    @PostMapping("/api/admin/role/info")
    public JsonResult getRoleService(@RequestBody Role role) {
       return JsonResult.buildSuccess(200,roleService.findRoleByRid(role.getId()));
    }

    /**
     *编辑角色
     * @return
     */
    @PutMapping("/api/admin/role")
    public JsonResult editRole(@RequestBody Role requestRole) {

        if (stringRedisTemplate.hasKey(AppConstant.DEFAULT_NAME_PREFIX)!=null){
            String token = stringRedisTemplate.opsForValue().get(AppConstant.DEFAULT_NAME_PREFIX);
            roleService.editRole(requestRole,token);
            return JsonResult.buildSuccess(200);
        }else {
            throw new DisabledLoginException("token不存在或已过期");
        }

    }


    /**
     *新增角色
     * @return
     */
    @PostMapping("/api/admin/role")
    public JsonResult addRole(@RequestBody RoleVo roleVo) {
        if (stringRedisTemplate.hasKey(AppConstant.DEFAULT_NAME_PREFIX)!=null) {
            String token = stringRedisTemplate.opsForValue().get(AppConstant.DEFAULT_NAME_PREFIX);
            roleService.addRole(roleVo, token);
            return JsonResult.buildSuccess(200, "操作成功");
        } else {
            throw new DisabledLoginException("token不存在或已过期");
        }

    }
    /**
     *更改角色信息
     * @return
     */
    @PostMapping("/api/admin/role/change")
    public JsonResult changeRole(@RequestBody ConstantVo constantVo) {

        if (stringRedisTemplate.hasKey(AppConstant.DEFAULT_NAME_PREFIX)!=null){
            String token = stringRedisTemplate.opsForValue().get(AppConstant.DEFAULT_NAME_PREFIX);
            roleService.changeRole(constantVo,token);
            return JsonResult.buildSuccess(200, "操作成功");
        }
        else {
            throw new DisabledLoginException("token不存在或已过期");
        }
    }

}
